package cn.yangliu.nacos.comm.config;

import java.util.Collections;

import static com.google.common.collect.Lists.newArrayList;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.plugins.DocumentationPluginsManager;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * The type Swagger 2 config.
 *
 * <p>
 * .
 *
 * @author 问道于盲
 * @date 2019 -04-17
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ConditionalOnClass({DocumentationPluginsManager.class, HandlerMapping.class})
public class Swagger2Config {

    /**
     * Api docket.
     *
     * @param apiInfo the api info
     * @return the docket
     */
    @Bean
    public Docket api(ApiInfo apiInfo) {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.apiInfo(apiInfo);
        ApiSelectorBuilder builder = docket.select();
        builder.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class));
        docket = builder.paths(PathSelectors.any()).build().securitySchemes(newArrayList(
                new ApiKey("token", "Authorization", "header")
        ));
        return docket;
    }


    /**
     * Api info api info.
     *
     * @return the api info
     */
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfo(
                "Api Server",
                "app接口服务,请求认证接口,需在header中添加Authorization",
                "1.0.0",
                "test",
                new Contact("admin", "admin", ""),
                "License of API", "API license URL", Collections.emptyList());
    }
}